package com.huaxin.device.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huaxin.device.common.BarcodeGenerator;
import com.huaxin.device.dto.ResponseResult;
import com.huaxin.device.dto.request.AddDeviceInstanceReqDTO;
import com.huaxin.device.dto.request.QueryDeviceInstanceListReqDTO;
import com.huaxin.device.dto.request.UpdateDeviceInstanceReqDTO;
import com.huaxin.device.models.DeviceInstance;
import com.huaxin.device.service.DeviceInstanceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "设备")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/device/deviceInstance")
public class DeviceInstanceController {
    private final DeviceInstanceService deviceInstanceService;

    /**
     * 添加设备实例
     *
     * @param in
     * @return
     */
    @Operation(summary = "添加设备实例", description = "添加设备实例")
    @PostMapping("/addDeviceInstance")
    public ResponseResult<String> addDeviceInstance(@RequestBody AddDeviceInstanceReqDTO in) {
        return deviceInstanceService.addDeviceInstance(in);
    }

    /**
     * 删除设备实例
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除设备实例", description = "删除设备实例")
    @GetMapping("/deleteDeviceInstance")
    public ResponseResult<String> deleteDeviceInstance(@Parameter(description = "设备实例ID", name = "id", required = true) @RequestParam(name = "id", required = true) String id) {
        return deviceInstanceService.deleteDeviceInstance(id);
    }

    /**
     * 修改设备实例
     *
     * @param in
     * @return
     */
    @Operation(summary = "修改设备实例", description = "修改设备实例")
    @PostMapping("/updateDeviceInstance")
    public ResponseResult<String> updateDeviceInstance(@RequestBody UpdateDeviceInstanceReqDTO in) {
        return deviceInstanceService.updateDeviceInstance(in);
    }

    /**
     * 查询设备实例
     *
     * @param id
     * @return
     */
    @Operation(summary = "查询设备实例", description = "查询设备实例")
    @GetMapping("/queryDeviceInstance")
    public ResponseResult<DeviceInstance> queryDeviceInstance(@Parameter(description = "设备实例ID", name = "id", required = true) @RequestParam(name = "id", required = true) String id) {
        DeviceInstance deviceInstance = deviceInstanceService.queryDeviceInstance(id);
        return ResponseResult.ok(deviceInstance);
    }

    @Operation(summary = "根据设备autoId查询设备实例", description = "根据设备autoId查询设备实例")
    @GetMapping("/queryDeviceInstanceByAutoId")
    public DeviceInstance queryDeviceInstanceByAutoId(@Parameter(description = "设备autoId", name = "autoId", required = true) @RequestParam(name = "autoId", required = true) Long autoId) {
        return deviceInstanceService.queryDeviceInstanceByAutoId(autoId);
    }

    /**
     * 查询设备实例列表
     *
     * @param in
     * @return
     */
    @Operation(summary = "查询设备实例列表", description = "查询设备实例列表")
    @PostMapping("/queryDeviceInstanceList")
    public ResponseResult<Page<DeviceInstance>> queryDeviceInstanceList(@RequestBody QueryDeviceInstanceListReqDTO in) {
        return deviceInstanceService.queryDeviceInstanceList(in);
    }

    /**
     * 根据设备sn查询设备信息
     *
     * @param sn 设备sn
     * @return 设备信息
     */
    @Operation(summary = "根据设备sn查询设备信息", description = "根据设备sn查询设备信息")
    @GetMapping("/queryDeviceInstanceBySn")
    public ResponseResult<DeviceInstance> queryDeviceInstanceBySn(@Parameter(description = "设备sn", name = "sn", required = true) @RequestParam(name = "sn", required = true) String sn) {
        return deviceInstanceService.queryDeviceInstanceBySn(sn);
    }

    /**
     * 根据设备sn生成条码
     *
     * @param sn 设备sn
     * @return 设备信息
     */
    @Operation(summary = "根据设备sn生成条码", description = "根据设备sn生成条码")
    @GetMapping("/generateBarcode")
    public ResponseResult<String> generateBarcode(@Parameter(description = "设备sn", name = "sn", required = true) @RequestParam(name = "sn", required = true) String sn) throws Exception {
        return ResponseResult.ok(BarcodeGenerator.generateBarcodeWithTextBase64(sn));
    }

    /**
     * 根据设备sn生成二维码
     *
     * @param sn 设备sn
     * @return 设备信息
     */
    @Operation(summary = "根据设备sn生成二维码", description = "根据设备sn生成二维码")
    @GetMapping("/generateQRCode")
    public ResponseResult<String> generateQRCode(@Parameter(description = "设备sn", name = "sn", required = true) @RequestParam(name = "sn", required = true) String sn) throws Exception {
        return ResponseResult.ok(BarcodeGenerator.generateQRCodeBase64(sn));
    }

    /**
     * 将设备与用户进行绑定
     *
     * @param sn         设备sn
     * @param customerId 用户ID
     * @param userType   用户类型 1-企业；2-家庭
     * @return 条码
     */
    @Operation(summary = "将设备与用户进行绑定", description = "将设备与用户进行绑定")
    @GetMapping("/bindDevice")
    public ResponseResult<String> bindDevice(@Parameter(description = "设备sn", name = "sn", required = true) @RequestParam(name = "sn", required = true) String sn,
                                             @Parameter(description = "用户ID", name = "customerId", required = true) @RequestParam(name = "customerId", required = true) String customerId,
                                             @Parameter(description = "用户类型 1-企业；2-家庭", name = "userType", required = true) @RequestParam(name = "userType", required = true) Integer userType) {
        return deviceInstanceService.bindDevice(sn, customerId, userType);
    }

    /**
     * 将设备与用户进行解绑
     *
     * @param sn 设备sn
     * @return 解绑结果
     */
    @Operation(summary = "将设备与用户进行解绑", description = "将设备与用户进行解绑")
    @GetMapping("/unbindDevice")
    public ResponseResult<String> unbindDevice(@Parameter(description = "设备sn", name = "sn", required = true) @RequestParam(name = "sn", required = true) String sn) {
        return deviceInstanceService.unbindDevice(sn);
    }

    /**
     * 分页查询某客户绑定的设备列表
     *
     * @param customerId 客户ID
     * @return 设备列表
     */
    @Operation(summary = "分页查询某客户绑定的设备列表", description = "分页查询某客户绑定的设备列表")
    @GetMapping("/queryDeviceInstanceListByCustomerId")
    public ResponseResult<Page<DeviceInstance>> queryDeviceInstanceListByCustomerId(@Parameter(description = "页码", name = "pageNum", required = true) @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                                                                    @Parameter(description = "每页数量", name = "pageSize", required = true) @RequestParam(name = "pageSize", required = true) Integer pageSize,
                                                                                    @Parameter(description = "客户ID", name = "customerId", required = true) @RequestParam(name = "customerId", required = true) String customerId) {
        return deviceInstanceService.queryDeviceInstanceListByCustomerId(pageNum, pageSize, customerId);
    }

    /**
     * 分页查询区域下的设备列表
     *
     * @param zoneId 客户ID
     * @return 设备列表
     */
    @Operation(summary = "分页查询区域下的设备列表", description = "分页查询区域下的设备列表")
    @GetMapping("/queryDeviceInstanceListByZoneId")
    public ResponseResult<Page<DeviceInstance>> queryDeviceInstanceListByZoneId(@Parameter(description = "页码", name = "pageNum", required = true) @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                                                                @Parameter(description = "每页数量", name = "pageSize", required = true) @RequestParam(name = "pageSize", required = true) Integer pageSize,
                                                                                @Parameter(description = "区域ID", name = "zoneId", required = true) @RequestParam(name = "zoneId", required = true) String zoneId) {
        return deviceInstanceService.queryDeviceInstanceListByZoneId(pageNum, pageSize, zoneId);
    }

    /**
     * 获取视频播放地址
     *
     * @param serialNumber 设备序列号
     * @return 视频播放地址
     */
    @Operation(summary = "获取视频播放地址", description = "获取视频播放地址")
    @GetMapping("/getVideoPlayUrl")
    public ResponseResult<String> getVideoPlayUrl(@Parameter(description = "设备序列号", name = "serialNumber", required = true) @RequestParam(name = "serialNumber", required = true) String serialNumber) {
        return deviceInstanceService.getVideoPlayUrl(serialNumber);
    }
}
